package com.atguigu.gulimall.member.service.impl;

import com.atguigu.gulimall.member.exception.PhoneException;
import com.atguigu.gulimall.member.exception.UserNameException;
import com.atguigu.gulimall.member.vo.SocialUser;
import com.atguigu.gulimall.member.vo.UserLoginVo;
import com.atguigu.gulimall.member.vo.UserRegVo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(UserRegVo vo) {
        //检查用户名 手机号是否唯一
        checkPhoneUnique(vo.getPhone());
        checkUserNameUnique(vo.getUserName());
        MemberEntity entity = new MemberEntity();
        entity.setLevelId(1L);
        entity.setMobile(vo.getPhone());
        entity.setUsername(vo.getUserName());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        entity.setPassword(encode);
        this.baseMapper.insert(entity);
    }

    @Override
    public void checkUserNameUnique(String userName) throws UserNameException {
        Long cnt = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (cnt > 0) {
            throw new UserNameException();
        }
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneException {
        Long cnt = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (cnt > 0) {
            throw new PhoneException();
        }
    }

    @Override
    public MemberEntity login(UserLoginVo vo) {
        String acct = vo.getLoginacct();
        String pwd = vo.getPassword();
        MemberEntity entity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", acct).or().eq("mobile", acct));
        if(entity==null){
            return null;
        }
        String pwdDb=entity.getPassword();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean matches = passwordEncoder.matches(pwd, pwdDb);
        return matches?entity:null;
    }

    @Override
    public MemberEntity oauthlogin(SocialUser vo) {
        String uid=vo.getUid();
        String token=vo.getAccess_toke();
        //TODO:根据uid获取用户，如果存在，则更新token和过期时间。
        // 不存在则注册新用户。需要调用第三方api，用token获取用户公开信息
        return null;
    }

}
